package helper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.TimeZone;

import data.ArticleID;
import data.Fact;
import data.ShopID;
import data.TagID;

public class Transformator {

private static Transformator _instance = null;
	
	private Transformator(){}
	
	/**
	 * Liefert Instant des Managers
	 * 
	 * @return DB2ConnectionManager
	 */
	public static Transformator getInstance() {
		if (_instance == null) {
			_instance = new Transformator();
		}
		return _instance;
	}
	
	
	/**
	 * Macht aus dem Resultset der Artikel eine Collection mit Artikel-Objekten
	 * @param articles
	 * @return
	 */
	public Collection<ArticleID> transformArticle(ResultSet articles) {
		
		System.out.print("Transform Articles gestartet...");
		
		Collection<ArticleID> result = new ArrayList<ArticleID>();
		
		
		try {
			while(articles.next()) {
				
				ArticleID article = new ArticleID(articles.getInt("articleid"));
				article.setName(articles.getString("name"));
				article.setPreis(articles.getDouble("preis"));
				article.setProductgroup(articles.getString("productgroup"));
				article.setProductfamily(articles.getString("productfamily"));
				article.setProductcategory(articles.getString("productcategory"));
				
				result.add(article);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		System.out.println("beendet!");
		
		return result;
		
	}
	
	/**
	 * Macht aus dem Resultset der Shops eine Collection mit Shop-Objekten
	 * @param shops
	 * @return
	 */
	public Collection<ShopID> transformShop(ResultSet shops) {
		
		System.out.print("Transform Shops gestartet...");
		
		Collection<ShopID> result = new ArrayList<ShopID>();
		
		
		try {
			while(shops.next()) {
				
				ShopID shop = new ShopID(shops.getInt("shopid"));
				shop.setName(shops.getString("name"));
				shop.setStadt(shops.getString("stadt"));
				shop.setRegion(shops.getString("region"));
				shop.setLand(shops.getString("land"));
				
				result.add(shop);
			}
			
			shops.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		System.out.println("beendet!");
		
		return result;
		
	}
	
	/**
	 * Erstellt aus csv Daten die Tag-Objekte
	 * @param tage
	 * @return
	 */
	public Collection<TagID> transformTag(Collection<String[]> tage) {
		
		System.out.print("Transform Tage gestartet...");
		
		Collection<TagID> result = new ArrayList<TagID>();
		Date date = new Date();
		
	    TimeZone tz = TimeZone.getDefault ();
	    Calendar c = Calendar.getInstance (tz);

		for(String[] s : tage) {

			SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yy");
			
			try {

				date = dateFormat.parse(s[0]);

			} catch (ParseException e) {
				e.printStackTrace();
			}
			
			
			if (!date.equals(c.getTime())) {
				
				c.setTime(date);
			
				TagID tag = new TagID();
				tag.setDatum(date);
				tag.setJahr(c.get(Calendar.YEAR));
				
				int month = c.get(Calendar.MONTH);
				
				 switch (month)
	             {
	               case Calendar.JANUARY: {
	            	   
	                   tag.setMonat("Januar");
	            	   tag.setQuartal("1. Quartal");
	            	   break;
	               }
	               case Calendar.FEBRUARY: {
	            	   tag.setMonat("Februar");
	            	   tag.setQuartal("1. Quartal");
	            	   break;
	               }
	               case Calendar.MARCH: {
	            	   tag.setMonat("März");
	            	   tag.setQuartal("1. Quartal");
	            	   break;
	               }
	               case Calendar.APRIL: {
	            	   tag.setMonat("April");
	            	   tag.setQuartal("2. Quartal");
	            	   break;
	               }
	               case Calendar.MAY: {
	            	   tag.setMonat("Mai");
	            	   tag.setQuartal("2. Quartal");
	            	   break;
	               }
	               case Calendar.JUNE: {
	            	   tag.setMonat("Juni");
	            	   tag.setQuartal("2. Quartal");
	            	   break;
	               }
	               case Calendar.JULY: {
	            	   tag.setMonat("Juli");
	            	   tag.setQuartal("3. Quartal");
	            	   break;
	               }
	               case Calendar.AUGUST: {
	            	   tag.setMonat("August");
	            	   tag.setQuartal("3. Quartal");
	            	   break;
	               }
	               case Calendar.SEPTEMBER: {
	            	   tag.setMonat("September");
	            	   tag.setQuartal("3. Quartal");
	            	   break;
	               }
	               case Calendar.OCTOBER: {
	            	   tag.setMonat("Oktober");
	            	   tag.setQuartal("4. Quartal");
	            	   break;
	               }
	               case Calendar.NOVEMBER: {
	            	   tag.setMonat("November");
	            	   tag.setQuartal("4. Quartal");
	            	   break;
	               }
	               case Calendar.DECEMBER: {
	            	   tag.setMonat("Dezember");
	            	   tag.setQuartal("4. Quartal");
	            	   break;
	               }
	             }
				 
				 result.add(tag);
			}
		}
		
		System.out.println("beendet!");
		
		return result;
		
	}
	
	/**
	 * Erstellt Collection der Fakten für die Faktentabelle
	 * @param articles
	 * @param shops
	 * @param tage
	 * @param sales
	 * @return
	 */
	public Collection<Fact> createFacts(Collection<ArticleID> articles, Collection<ShopID> shops, Collection<TagID> tage, Collection<String[]> sales) {
		
		System.out.print("Transform Facts gestartet...");
		
		Collection<Fact> facts = new ArrayList<Fact>();
		
		for(String[] s : sales) {
			
			Fact fact = new Fact();
			
			for(ArticleID a : articles) {
				
				
				if(s[2].equals(a.getName())) {
					fact.setArticle(a);
				}
				
			}
			
			for(ShopID sh : shops) {
				
				if(s[1].equals(sh.getName())) {
					fact.setShop(sh);
				}
			}
			
			SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yy");
			Date date = new Date();
			
			try {

				date = dateFormat.parse(s[0]);

			} catch (ParseException e) {
				e.printStackTrace();
			}
			
			for(TagID t : tage) {
				
				if(date.equals(t.getDatum())) {
					fact.setTag(t);
				}
			}
			
			fact.setVerkauft(Integer.valueOf(s[3]));
			
			String umsatz = s[4].replace(',', '.');
			
			fact.setUmsatz(Double.valueOf(umsatz));
			
			facts.add(fact);
			
		}
		
		System.out.println("beendet!");
		
		return facts;
		
	}
	
	
}
